# encoding: utf-8
import csv
import glob
import sys
import os
from dbfpy import dbf


def function():
    print glob.glob(r'*.py')  # 支持相对路径与绝对路径，以及通配符? * []
    for path in glob.iglob(r'*.py'):  # 生成器
        print path


def get_sys_args():
    # for item in sys.argv: #取出执行时输入参数
    # print item
    print sys.argv[0]


# dbf文件转换csv
def dbf_to_csv(from_file, to_dir):
    csv_dir, file_name = os.path.split(from_file)
    csv_name = to_dir + os.sep + os.path.splitext(file_name)[0] + ".csv"
    print "csvName:", csv_name
    writer = csv.writer(open(csv_name, "wb"))
    db = dbf.Dbf(from_file)
    writer.writerow(db.fieldNames)
    [writer.writerow(record.fieldData) for record in db]


def unzip():
    pass

if __name__ == '__main__':
    # function()
    #get_sys_args()
    form_file = 'G:\PythonWorkSpace\Test\dbf\SJSTJ0316.dbf'
    to_dir = os.path.split(form_file)[0]
    dbf_to_csv(form_file, to_dir)